#include "mts_manager.h"

#include <QtGui/QMessageBox>
#include "QSMSControl.h"
#include "qenterpin.h"
#include "qenternumpaycard.h"
#include "windows.h"

QFrame * currentOpenFrame = NULL;



mts_manager::mts_manager(QWidget *parent)
    : QWidget(parent), isConnected_( false ), mousePressed( false ), mouseMoved( false ),
      mouseReleased( false )
{
	ui.setupUi(this);

	timerApp = new QTimer(this);

	QPoint newWndPosition( x() +100, y() +100);
	move( mapToGlobal( newWndPosition ));

	// ui.btMainCheckBalance->setFlat(true);

	// ui.mainPage->setStyleSheet(QString::fromUtf8("#mainStackedWidget{\n"
	//		"background-image: url(:/mainWnd/img/emptyFrame.PNG)\n"
	//		"}\n"
	//		""));
	ui.mainStackedWidget->setCurrentIndex( 0 );
	ui.stwSettings2->setCurrentIndex( 0 );
	fillSettingsList();

	wndEnterPIN = new QEnterPIN(this);
	QPoint point( ui.btMainCheckBalance->x()+20, ui.btMainCheckBalance->y()+60 );
	wndEnterPIN->move( mapToGlobal(point));

	wndCheckUpdates= new CheckUpdates(this);
	point = QPoint( ui.btMainCheckBalance->x()+20, ui.btMainCheckBalance->y()+60 );
	wndCheckUpdates->move( mapToGlobal(point));

	ui.chbConnections->addItem( tr("МТС Коннект не подключен") );

	ui.frameConnected->setObjectName( QString::fromUtf8("frameConnected"));
	ui.frameConnected->setVisible( false );

	ui.leSMSCenterPhone->setFrame( true );

	ui.btConnection->setObjectName( QString::fromUtf8( "btConnection" ));

	connect(timerApp, SIGNAL(timeout()), this, SLOT( CloseWnd() ));
}

mts_manager::~mts_manager()
{
	delete wndEnterPIN;

}
void mts_manager::paintEvent(QPaintEvent * event ){

		QPainter painter( this );

		QBitmap bitmap(".//img/mainImg.png" );
		QRegion region( bitmap);
		QRect boundRect = region.boundingRect();

		setMask( region.boundingRect());
}
void mts_manager::OnMainCheckBalance(){

	wndEnterPIN->setType( QEnterPIN::PIN );
	wndEnterPIN->getLEPinCode()->clear();
	wndEnterPIN->show();

}
void mts_manager::OnMainActivateUpdates(){

	ui.mainStackedWidget->setCurrentWidget( ui.pageActivateUpdates );

}
void mts_manager::OnMainCheckUpdates(){

	wndCheckUpdates->show();
}
void mts_manager::OnMainActivatePayCard(){

	QEnterNumPayCard * enterNumPayCard = new QEnterNumPayCard();

	QPoint point( ui.btMainActivatePayCard->x()+20, ui.btMainActivatePayCard->y()+60 );
	enterNumPayCard->move( mapToGlobal(point));
	enterNumPayCard->show();

}
void mts_manager::OnMainConnect(){

	if( !isConnected() ){
		QMessageBox::information( this,tr("Информация"),tr("Устанавливаю подключение..."),1);
		setConnected( true );

		ui.mainStackedWidget->setCurrentWidget( ui.mainPageConnected );
		ui.btConnection->setProperty("connected", true);
		ui.frameConnected->setProperty("connected", true);
		setConnected( true );
	}else{
		QMessageBox::information( this,tr("Информация"),tr("Отключение..."),1);
		ui.chbConnections->setItemText( 0, tr("МТС Коннект не подключен к 3G") );
		ui.btConnection->setProperty("connected", false);
		ui.frameConnected->setProperty("connected", false);
		setConnected( false );
	}

	ui.frameConnected->setVisible( true );
	ui.frameConnected->setStyleSheet(ui.frameConnected->styleSheet());
	ui.btConnection->setStyleSheet(ui.btConnection->styleSheet());
}
void mts_manager::OnUpdateSoft1(){

	QFrame * frame = new QFrame(this);
	QBitmap bitmap(".//img/wndActivationResults.png" );
	QRegion region( bitmap);
	QPoint point( ui.bt1->x()+20, ui.bt1->y()+60 );

	frame->setMask( region.boundingRect());
	frame->setObjectName( QString::fromUtf8( "frameActivation" ));
	frame->setWindowFlags(Qt::SplashScreen);
	frame->move( mapToGlobal(point));

	currentOpenFrame = frame;

	timerApp->start(3000); //wait for 3 secs to close the form

	frame->show();

	if( isConnected() ){
		ui.mainStackedWidget->setCurrentWidget( ui. mainPageConnected );
	}else
		ui.mainStackedWidget->setCurrentWidget( ui. mainPage );

}
void mts_manager::CloseWnd(){

	if( currentOpenFrame != NULL )
		currentOpenFrame->close();
}

void mts_manager::OnSettings(){

	//QMessageBox::information( this,tr("Info"),tr("Open settings tab"),1);
	ui.mainStackedWidget->setCurrentWidget( ui.pageSettings );
}
void mts_manager::OnConnect(){

	ui.mainStackedWidget->setCurrentWidget( ui.mainPage );
	if( isConnected() ){
		ui.chbConnections->setItemText(0, tr("МТС Коннект подключен к 3G") );
	}else {
		ui.chbConnections->setItemText(0, tr("МТС Коннект не подключен к 3G") );

	}
	ui.frameConnected->setVisible( true );
}

void mts_manager::fillSettingsList(){

	lwItemStartupParams = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemStartupParams->setItemText( tr("  Параметры запуска") );

	lwItemInterfaceSettings = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemInterfaceSettings->setItemText( tr("  Настройка интерфейса") );


	lwItemModemSettings = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemModemSettings->setItemText( tr("  Модем") );

	lwItemNet = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemNet->setItemText( tr("  Сеть") );

	lwItemPIN = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemPIN->setItemText( tr("  Операции с PIN") );

	lwItemSMS = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemSMS->setItemText( tr("  SMS") );

	lwItemRecordBook = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemRecordBook->setItemText( tr("  Записная книга") );

	lwItemVoiceCalls = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemVoiceCalls->setItemText( tr("  Голосовые вызовы") );

	lwItemAbout = new QListWidgetItemSpec( ui.lwSettingsList );
	lwItemAbout->setItemText( tr("  О программе") );

}
void mts_manager::OnEditPIN(){

	wndEnterPIN->setType( QEnterPIN::PIN );
	wndEnterPIN->show();
}
void mts_manager::OnEnterPUK(){

	wndEnterPIN->setType( QEnterPIN::PUK );
	wndEnterPIN->show();
}
void mts_manager::OnSMS(){
	ui.mainStackedWidget->setCurrentWidget(ui.pageSMS);
	QSMSControl::getInstance( this );
}

void mts_manager::OnSMSItemClicked(QTreeWidgetItem *item, int)
{
	QSMSControl &SMSControl = QSMSControl::getInstance(this);
	SMSControl.OnItemExpanded(item);
}

void mts_manager::OnSettingsItemChanged(QListWidgetItem *current, QListWidgetItem *previous)
{
	if( current == lwItemStartupParams){
		ui.stwSettings2->setCurrentWidget( ui.pageStartupParams );
	}
	else if(current == lwItemInterfaceSettings){
		ui.stwSettings2->setCurrentWidget( ui.pageSettingsIntf );
	}
	else if(current == lwItemModemSettings){
			ui.stwSettings2->setCurrentWidget( ui.pageModem );
	}
	else if (current == lwItemNet){
		ui.stwSettings2->setCurrentWidget(ui.pageNetwork);
	}
	else if( current == lwItemPIN){
			ui.stwSettings2->setCurrentWidget( ui.pagePINPUK );
	}
	else if( current == lwItemSMS){
			ui.stwSettings2->setCurrentWidget( ui.pageSMS1 );
	}
	else if( current == lwItemRecordBook){
			ui.stwSettings2->setCurrentWidget( ui.pagePhoneBook );
	}
	else if( current == lwItemVoiceCalls){
			ui.stwSettings2->setCurrentWidget( ui.pageCalls1 );
	}
	else if( current == lwItemAbout){
			ui.stwSettings2->setCurrentWidget( ui.pageAbout);
	}

}

QTreeWidget* mts_manager::getLeftTree(){

	return ui.trLeftTree;
}

void mts_manager::On100Mb(){


}
void mts_manager::On250Mb(){

	QFrame * frame1 = new QFrame(this);
	QBitmap bitmap(".//img/ussd_activation_results.png" );
	QRegion region( bitmap);
	QRect boundRect = region.boundingRect();

	frame1->setMask( region.boundingRect());
	frame1->setObjectName( QString::fromUtf8( "frameUSSDActivation" ) );
	frame1->setWindowFlags(Qt::SplashScreen);
	frame1->setStyleSheet( QString::fromUtf8( "#frameUSSDActivation{ background-image: url(:/mainWnd/img/ussd_activation_results.png)}") );

	currentOpenFrame = frame1;

	QPoint point( ui.bt250Mb->x()+20, ui.bt250Mb->y()-60 );
	frame1->move( mapToGlobal(point));

	timerApp->start( 3000 );
	frame1->show();
}

void mts_manager::On500Mb(){

}
void mts_manager::On1Gb(){

}
void mts_manager::OnAbout_UpdateProgram(){

	ui.stwSettings2->setCurrentWidget( ui.pageAboutUpdateProgram );

//TODO set button picture on btCheckNow;


}

void mts_manager::OnStatsTransparencyChanged(int value)
{
	QString fmt(tr("%1%"));
	ui.lblStatsTransparencyVal->setText( fmt.arg(value) );
}

void mts_manager::OnCalls(){
	ui.mainStackedWidget->setCurrentWidget( ui.pageCalls );

}
void mts_manager::OnContactClicked( QListWidgetItem* item ){

	//do something
	if( item->text() != "new contact" ){
		ui.leMobile->setText( tr( "+7 (916)222 11 33" ) );
		ui.leWorkPhone->setText( tr("+7 (495) 333 11 22") );
		ui.leEMail->setText ( tr("kenny@youbastards.com") );
	}
}
void mts_manager::OnAddContact(){

	QListWidgetItem *  newContact = new QListWidgetItem;
	ui.lwContactsList->addItem( newContact );

	newContact->setText( tr("new contact") );

	ui.leMobile->clear();
	ui.leWorkPhone->clear();
	ui.leEMail->clear();

	ui.frameContactPic->setStyleSheet( QString::fromUtf8( "#frameContactPic{ border-image: url(:/mainWnd/img/addPics.PNG)}") );
//TODO add pressed and hover png for btSMS2, btAddContact, btDeleteContact, btCall
}
void mts_manager::mousePressEvent( QMouseEvent * event ){

	QPoint cursor = event->globalPos();

	mousePressed = true;
}
void mts_manager::mouseMoveEvent( QMouseEvent * event ){

	mouseMoved = true;
}
void mts_manager::mouseReleaseEvent( QMouseEvent * event ){

	if( mousePressed && mouseMoved ){
		mousePressed = false;
		mouseMoved = false;

		move( event->globalPos() );

	}
}
void mts_manager::dragMoveEvent( QDragMoveEvent* event ){

	//QMessageBox::information( this,tr("Info"),tr("wwww"),1);

}
